// Load the data.
clear all
use "$pathD/all_tract.dta", clear

// Define the variables.
local vars PM_2p5_indoor PM_2p5_outdoor

// Keep no smoke days.
drop if smokepm_pred > 0

// Means
tempname postMeans
tempfile means
postfile `postMeans' str100 varname str100 richMeans str100 poorMeans ///
	str100 Diffs using "`means'", replace
foreach v of local vars {
    local name: variable label `v'
    preserve
	keep if above_med_income
	sum `v'
	local richmean: display %9.3f r(mean)
	local richsd: display %9.3f r(sd)
	restore
    preserve
	drop if above_med_income
	sum `v'
	local poormean: display %9.3f r(mean)
	local poorsd: display %9.3f r(sd)
	restore
	ttest `v', by(above_med_income)
	local diffmean: display %12.3fc r(mu_1) - r(mu_2)
	local diffse: display %12.3fc r(se)
    post `postMeans' ("`name'") ("`richmean'") ("`poormean'") ("`diffmean'") 
	post `postMeans' ("") ("`richsd'") ("`poorsd'") ("`diffse'")
	}
postclose `postMeans'

// N
tempname postN
tempfile N
postfile `postN' str100 varname str100 richN str100 poorN ///
	using "`N'", replace
preserve
keep if above_med_income
unique tract_id
local richT: display %12.0gc r(sum)
local richN: display %12.0gc r(N)
restore
preserve
drop if above_med_income
unique tract_id
local poorT: display %12.0gc r(sum)
local poorN: display %12.0gc r(N)
restore
post `postN' ("Census tracts") ("`richT'") ("`poorT'")
post `postN' ("Observations") ("`richN'") ("`poorN'")
postclose `postN'

// Table means.
use `means', clear
gen odd = mod(_n, 2)
foreach var in richMeans poorMeans {
	replace `var' = subinstr(`var', " ", "", .)
	replace `var' = "(" + `var' + ")" if odd == 0 
	}
foreach var in Diffs {
	replace `var' = subinstr(`var', " ", "", .)
	replace `var' = "[" + `var' + "]" if odd == 0 
	}
drop odd
list
listtab * using "$pathR/Tables/summary_nosmoke.tex", ///
    rstyle(tabular) replace
	
// Table N.
use `N', clear
list
listtab * using "$pathR/Tables/summary_nosmoke_N.tex", ///
    rstyle(tabular) replace
	
// Load the data.
clear all
use "$pathD/all_tract.dta", clear

// Keep only low smoke days.
keep if med_smokepm_pred == 1

// Define the variables.
local vars PM_2p5_indoor PM_2p5_outdoor

// Means
tempname postMeans
tempfile means
postfile `postMeans' str100 varname str100 richMeans str100 poorMeans ///
	str100 Diffs using "`means'", replace
foreach v of local vars {
    local name: variable label `v'
    preserve
	keep if above_med_income
	sum `v'
	local richmean: display %9.3f r(mean)
	local richsd: display %9.3f r(sd)
	restore
    preserve
	drop if above_med_income
	sum `v'
	local poormean: display %9.3f r(mean)
	local poorsd: display %9.3f r(sd)
	restore
	ttest `v', by(above_med_income)
	local diffmean: display %12.3fc r(mu_1) - r(mu_2)
	local diffse: display %12.3fc r(se)
    post `postMeans' ("`name'") ("`richmean'") ("`poormean'") ("`diffmean'") 
	post `postMeans' ("") ("`richsd'") ("`poorsd'") ("`diffse'")

	}
postclose `postMeans'

// N
tempname postN
tempfile N
postfile `postN' str100 varname str100 richN str100 poorN ///
	using "`N'", replace
preserve
keep if above_med_income
unique tract_id
local richT: display %12.0gc r(sum)
local richN: display %12.0gc r(N)
restore
preserve
drop if above_med_income
unique tract_id
local poorT: display %12.0gc r(sum)
local poorN: display %12.0gc r(N)
restore
post `postN' ("Census tracts") ("`richT'") ("`poorT'")
post `postN' ("Observations") ("`richN'") ("`poorN'")
postclose `postN'

// Table means.
use `means', clear
gen odd = mod(_n, 2)
foreach var in richMeans poorMeans {
	replace `var' = subinstr(`var', " ", "", .)
	replace `var' = "(" + `var' + ")" if odd == 0 
	}
foreach var in Diffs {
	replace `var' = subinstr(`var', " ", "", .)
	replace `var' = "[" + `var' + "]" if odd == 0 
	}
drop odd
list
listtab * using "$pathR/Tables/summary_low_smoke.tex", ///
    rstyle(tabular) replace
	
// Table N.
use `N', clear
list
listtab * using "$pathR/Tables/summary_low_smoke_N.tex", ///
    rstyle(tabular) replace
	
// Load the data.
clear all
use "$pathD/all_tract.dta", clear

// Keep only high smoke days.
keep if med_smokepm_pred == 2

// Define the variables.
local vars PM_2p5_indoor PM_2p5_outdoor

// Means
tempname postMeans
tempfile means
postfile `postMeans' str100 varname str100 richMeans str100 poorMeans ///
	str100 Diffs using "`means'", replace
foreach v of local vars {
    local name: variable label `v'
    preserve
	keep if above_med_income
	sum `v'
	local richmean: display %9.3f r(mean)
	local richsd: display %9.3f r(sd)
	restore
    preserve
	drop if above_med_income
	sum `v'
	local poormean: display %9.3f r(mean)
	local poorsd: display %9.3f r(sd)
	restore
	ttest `v', by(above_med_income)
	local diffmean: display %12.3fc r(mu_1) - r(mu_2)
	local diffse: display %12.3fc r(se)
    post `postMeans' ("`name'") ("`richmean'") ("`poormean'") ("`diffmean'") 
	post `postMeans' ("") ("`richsd'") ("`poorsd'") ("`diffse'")

	}
postclose `postMeans'

// N
tempname postN
tempfile N
postfile `postN' str100 varname str100 richN str100 poorN ///
	using "`N'", replace
preserve
keep if above_med_income
unique tract_id
local richT: display %12.0gc r(sum)
local richN: display %12.0gc r(N)
restore
preserve
drop if above_med_income
unique tract_id
local poorT: display %12.0gc r(sum)
local poorN: display %12.0gc r(N)
restore
post `postN' ("Census tracts") ("`richT'") ("`poorT'")
post `postN' ("Observations") ("`richN'") ("`poorN'")
postclose `postN'

// Table means.
use `means', clear
gen odd = mod(_n, 2)
foreach var in richMeans poorMeans {
	replace `var' = subinstr(`var', " ", "", .)
	replace `var' = "(" + `var' + ")" if odd == 0 
	}
foreach var in Diffs {
	replace `var' = subinstr(`var', " ", "", .)
	replace `var' = "[" + `var' + "]" if odd == 0 
	}
drop odd
list
listtab * using "$pathR/Tables/summary_high_smoke.tex", ///
    rstyle(tabular) replace
	
// Table N.
use `N', clear
list
listtab * using "$pathR/Tables/summary_high_smoke_N.tex", ///
    rstyle(tabular) replace

clear
